﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 文档说明
{
    class core_文件相关_file
    {
        /*
          [HttpPost]
        [Route("SFiles")]
        public IActionResult SFiles(List<IFormFile> fff)
        {

            //var f = HttpContext.Request;
            var a = HttpContext.Request.Form.Files;
            // var a1 = HttpContext.Request.Form["fillll"] as List<IFormFile>;
            // List<IFormFile> dd = (List<IFormFile>)a1;
            //var a1 = HttpContext.Request.Form.GetEnumerator("file3");
            //var aaa = test.file3;
            //IFormCollection aaa = HttpContext.Request.Form["files"];
            //var dd = w;
            //if (file == null)
            //{
            //    return StatusCode(401, new
            //    {
            //        msg = "cuowu"
            //    });
            //}
            //var suffix = Path.GetExtension(test.file2.FileName);//提取上传的文件文件后缀
            //{
            //    using (FileStream fs = System.IO.File.Create("./fileqqq/aaa.txt"))//注意路径里面最好不要有中文
            //    {
            //        test.file2.CopyTo(fs);//将上传的文件文件流，复制到fs中
            //        fs.Flush();//清空文件流
            //    }
            //}
            return Ok();
        }
        [HttpPost]
        [Route("DFiles")]
        public IActionResult DFiles()
        {
            // string CurrentPath = System.Web.HttpContext.Current.Server.MapPath(fileurl);//获取物理路径
            // var filepath = Path.Combine(CurrentPath, filesavename);//文件的完整路径
            //整合绝对物理路径,获取的地址为manager的系统地址，file 的保存地址需要转web 地址，用replace 替换字符串。
            // string FilePath = (UploadFileParam.StudyDataPath + DownFile.save_name).Replace("JYZX.ZhuHaiTrain.Web", "JYZX.ZhuHaiTrain.Manager");   //replace(old , new) 
            //string FilePath = Path.Combine(CurrentPath, "./fileqqq/aaa.txt"); //指出文件的绝对路径
            return File(new FileStream("./fileqqq/aaa.txt", FileMode.Open), "application/octet-stream", "aaaaaa.text");//编码Server.UrlEncode(filename)
        }
        **************************************************************
        * 文件上传需要js一些配置
        js代码
        var myAxios = {};
myAxios.install = function(Vue) {
    var fetch = function(url, data, type) {
        data = data ? data : {};
        type = type.toUpperCase();
        return new Promise(function(resolve, reject) {
            var dataStr = ''; //数据拼接字符串
            var sendData = {};
            if (type == 'GET' || type == 'DELETE') {
                Object.keys(data).forEach(function(key) {
                    if (typeof(data[key]) == "object") {
                        sendData = data[key];
                    } else {
                        dataStr += key + '=' + data[key] + '&';
                    }
                })

                if (dataStr !== '') {
                    dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
                    url = url + '?' + dataStr;
                }
            } else {
                sendData = data;
            }
            var requestObj;
            if (window.XMLHttpRequest) {
                requestObj = new XMLHttpRequest();
            } else {
                requestObj = new ActiveXObject();
            }
            requestObj.open(type, url, true);
            requestObj.setRequestHeader("Accept", "application/json");
            requestObj.setRequestHeader("Authorization", 'Bearer ' + window.VueRootInstance.myStorage.getToken());
            requestObj.setRequestHeader("content-type", "application/json");
            requestObj.send(JSON.stringify(sendData));
            requestObj.onreadystatechange = function() {
                if (requestObj.readyState == 4) {
                    var obj = requestObj.response;
                    if (obj && typeof obj !== 'object') {
                        try {
                            obj = JSON.parse(obj);
                        } catch (error) {
                            reject({
                                success: false,
                                msg: "",
                                data: null
                            });
                            return;
                        }
                    }
                    //api返回数据-成功
                    if (requestObj.status == 200) {
                        resolve(obj);
                    }
                    //api返回数据-失败-token过期
                    else if (requestObj.status == 401 || requestObj.status == 4010) {
                        resolve({
                            success: false,
                            msg: "当前用户登录信息已过期！",
                            data: obj
                        });
                        setTimeout(function() {
                            //window.top.VueRootInstance.$storage.removeUserInfo();
                            window.top.VueRootInstance.myStorage.removeToken();
                            //window.top.VueRootInstance.$loginService.logout();
                        }, 200);
                    }
                    //api返回数据-失败-无权限
                    else if (requestObj.status == 405) {
                        resolve({
                            success: false,
                            msg: "当前用户登录无权限访问！",
                            data: obj
                        });
                    }
                    //api返回数据-失败-未知错误
                    else {
                        reject({
                            success: false,
                            msg: "服务器异常，请稍后重试！",
                            data: obj
                        });
                    }
                }
            }
        });
    };
    //带文件
    var fetchWithFile = function(url, data, type) {
        data = data ? data : {};
        type = type.toUpperCase();
        return new Promise(function(resolve, reject) {
            var dataStr = ''; //数据拼接字符串
            var sendData = {};
            //自己做formdata
            sendData = objToFormData(data);
            var requestObj;
            if (window.XMLHttpRequest) {
                requestObj = new XMLHttpRequest();
            } else {
                requestObj = new ActiveXObject();
            }
            requestObj.open(type, url, true);
            requestObj.setRequestHeader("Accept", "application/json");
            requestObj.setRequestHeader("Authorization", 'Bearer ' + window.VueRootInstance.myStorage.getToken());
            requestObj.send(sendData);
            requestObj.onreadystatechange = function() {

                if (requestObj.readyState == 4) {

                    var obj = requestObj.response;
                    if (obj && typeof obj !== 'object') {
                        try {
                            obj = JSON.parse(obj);
                        } catch (error) {
                            reject({
                                success: false,
                                msg: "",
                                data: null
                            });
                            return;
                        }
                    }
                    //api返回数据-成功
                    if (requestObj.status == 200) {
                        resolve(obj);
                    }
                    //api返回数据-失败-token过期
                    else if (requestObj.status == 401 || requestObj.status == 4010) {
                        resolve({
                            success: false,
                            msg: "当前用户登录信息已过期！",
                            data: obj
                        });
                        setTimeout(function() {
                            //window.top.VueRootInstance.$storage.removeUserInfo();
                            window.top.VueRootInstance.myStorage.removeToken();
                            //window.top.VueRootInstance.$loginService.logout();
                        }, 200);
                    }
                    //api返回数据-失败-无权限
                    else if (requestObj.status == 405) {
                        resolve({
                            success: false,
                            msg: "当前用户登录无权限访问！",
                            data: obj
                        });
                    }
                    //api返回数据-失败-未知错误
                    else {
                        reject({
                            success: false,
                            msg: "服务器异常，请稍后重试！",
                            data: obj
                        });
                    }
                }
            }
        });
    };
    var objToFormData = function(jsObj) {
        let obj = jsObj;
        let formData = new FormData();
        for (var i in obj) {
            console.log(i);
            console.log(Object.prototype.toString.call(obj[i]));
            //文件数组
            if (Object.prototype.toString.call(obj[i]) == '[object FileList]') {
                for (var j in obj[i]) {
                    // if(Object.prototype.toString.call(obj[i])== '[object Object]'){
                    // }
                    formData.append(i, obj[i][j]);
                }
            } else {
                formData.append(i, obj[i]);
            }
        }
        return formData;
    }
    Vue.prototype.myAxios = {
        get: function(url, data) {
            url = window.VueRootInstance.myConfig.get("baseUrl") + url;
            return fetch(url, data, "GET");
        },
        post: function(url, data) {
            url = window.VueRootInstance.myConfig.get("baseUrl") + url;
            return fetch(url, data, "POST");
        },
        postWithFile: function(url, data) {
            url = window.VueRootInstance.myConfig.get("baseUrl") + url;
            return fetchWithFile(url, data, "POST");
        },
        put: function(url, data) {
            url = window.VueRootInstance.myConfig.get("baseUrl") + url;
            return fetch(url, data, "PUT");
        },
        putWithFile: function(url, data) {
            url = window.VueRootInstance.myConfig.get("baseUrl") + url;
            return fetchWithFile(url, data, "PUT");
        },
        delete: function(url, data) {
            url = window.VueRootInstance.myConfig.get("baseUrl") + url;
            return fetch(url, data, "DELETE");
        },
        postLogin: function(url, data) {
            url = window.VueRootInstance.myConfig.get("loginUrl") + url;
            return fetch(url, data, "POST");
        },
    };
};
module.exports = myAxios;
         */
    }
}
